<?php

include_once(dirname(__FILE__) . "/config.php");
session_start();

$folders = array(
"data_access",
"business",
"lib"
);

$publicPages = array(
"login",
"actions/user/login",
"service"
);

foreach ($folders as $folder){
	includeFolder($folder);
}


$files = array(
"language/" . getLanguage() . ".php" 	
);


foreach ($files as $file){
	include_once(PATHROOT. "/" .$file);
}

$public = false;
if(curPageURL() == WWWROOT){
	$public = true;
}
if (!$public){
	foreach ($publicPages as $publicPage){
		if (contains(curPageURL(), WWWROOT.$publicPage)){
			$public = true;
		}
	}
}	

if(contains(curPageURL(), WWWROOT."admin") || contains(curPageURL(), WWWROOT."actions/admin")){	
	if(!isAdminLoggedIn()){	
		logError(langEcho("permission:access:denied"));
		forward(WWWROOT . "login.php?url=" . urlencode(curPageURL()));
	}
}else{
	if (!isUserLoggedIn()){
		if (!$public){
			forward(WWWROOT . "login.php?url=" . urlencode(curPageURL()));
		}
	}
}


if(isset($_REQUEST["f"])){
	$file = $_REQUEST["f"];
	if(is_dir($file)){
		if ($file[strlen($file) -1] != "/"){
			$file .= "/";
		}
		$file .= "index.php";  
	}
	if($file == ""){
		$file = "index.php";
	}
	if(file_exists($file)){	
		include_once($file);
	}
}

function getDirectoryList ($directory) 
  {   
    $results = array();  
    $handler = opendir($directory);
    while ($file = readdir($handler)) {     
      if ($file != "." && $file != ".." && $file != ".svn") {
        $results[] = $file;
      }
    }   
    closedir($handler);   
    return $results;
  }

  
function includeFolder($folder){	
	$files = getDirectoryList(PATHROOT .$folder);
	foreach ($files as $file){				
		if(is_dir(PATHROOT . $folder . "/". $file)){
			includeFolder($folder. "/" .$file);
		}else{
			include_once(PATHROOT . $folder . "/". $file);
		}
	}
}

?>